package com.org.web.support.config;

import com.org.web.handle.error.MyErrorPageRegistrar;
import com.org.web.support.conponents.Web;
import com.org.web.support.interceptors.AccessInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/** @author devg @Date 2020/2/26 18:51 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

  @Autowired
  private Web web;

  @Autowired
  private AccessInterceptor accessInterceptor;

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    String[] filterUrlsArray =  web.getFilterUrls();
    String[] noFilterUrlsArray = web.getNoFilterUrls();
    registry.addInterceptor(accessInterceptor).addPathPatterns(filterUrlsArray).excludePathPatterns(noFilterUrlsArray);
  }

  @Bean
  public ErrorPageRegistrar errorPageRegistrar(){
    return new MyErrorPageRegistrar();
  }

  @Bean
  public RestTemplate restTemplate() {
    final int connectTimeout = 6000;
    final int readTimeout = 6000;
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setConnectTimeout(connectTimeout);
    requestFactory.setReadTimeout(readTimeout);
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    return restTemplate;
  }

}
